<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="文章" prop="columnId">
        <el-select v-model="queryParams.columnId" placeholder="请选择文章" clearable>
          <el-option
            v-for="item in columnList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="评论人"
        prop="commentUserName"
      >
        <el-input
          v-model="queryParams.commentUserName"
          placeholder="请输入评论人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['model:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['model:comment:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['model:comment:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['model:comment:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="文章" align="center" prop="columnId" /> -->
      <!-- <el-table-column label="模型id" align="center" prop="modelId" /> -->
      <el-table-column
        label="评论内容"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="评论人"
        align="center"
        prop="commentUserName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="评论时间"
        align="center"
        prop="commentTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.commentTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="评论人ID" align="center" prop="commentUserId" /> -->
      <el-table-column
        label="评论来源"
        align="center"
        prop="source"
      >
        <template v-slot="scope">
          <dict-tag
            :options="dict.type.source"
            :value="scope.row.source"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="审核人"
        align="center"
        prop="examineUserName"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="examineStatus"
      >
        <template v-slot="scope">
          <dict-tag
            :options="dict.type.examine_status"
            :value="scope.row.examineStatus"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="审核时间"
        align="center"
        prop="examineTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.examineTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="显示状态"
        align="center"
        prop="status"
      >
        <template v-slot="scope">
          <dict-tag
            :options="dict.type.show_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:comment:edit']"
          >审核</el-button> -->

          <el-popconfirm
            icon="el-icon-info"
            icon-color="red"
            @confirm="reviewComment(scope.row,scope.index,true)"
            @cancel="reviewComment(scope.row,scope.index,false)"
            confirm-button-text='通过'
            cancel-button-text='不通过'
            title="请审核该评论是否通过？"
          >
            <el-button
              icon="el-icon-circle-check"
              slot="reference"
              v-if="scope.row.examineStatus != 2"
              size="mini"
              type="text"
            >审核 </el-button>
          </el-popconfirm>
          <el-button
            size="mini"
            type="text"
            style="margin-left: 10px;"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="文章id"
          prop="columnId"
        >
          <el-input
            v-model="form.columnId"
            placeholder="请输入文章id"
          />
        </el-form-item>
        <el-form-item
          label="模型id"
          prop="modelId"
        >
          <el-input
            v-model="form.modelId"
            placeholder="请输入模型id"
          />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor
            v-model="form.content"
            :min-height="192"
          />
        </el-form-item>
        <el-form-item
          label="评论时间"
          prop="commentTime"
        >
          <el-date-picker
            clearable
            v-model="form.commentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择评论时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="评论人ID"
          prop="commentUserId"
        >
          <el-input
            v-model="form.commentUserId"
            placeholder="请输入评论人ID"
          />
        </el-form-item>
        <el-form-item
          label="评论人姓名"
          prop="commentUserName"
        >
          <el-input
            v-model="form.commentUserName"
            placeholder="请输入评论人姓名"
          />
        </el-form-item>
        <el-form-item
          label="来源1门户"
          prop="source"
        >
          <el-input
            v-model="form.source"
            placeholder="请输入来源1门户"
          />
        </el-form-item>
        <el-form-item
          label="审核人ID"
          prop="examineUserId"
        >
          <el-input
            v-model="form.examineUserId"
            placeholder="请输入审核人ID"
          />
        </el-form-item>
        <el-form-item
          label="审核时间"
          prop="examineTime"
        >
          <el-date-picker
            clearable
            v-model="form.examineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComment,
  getComment,
  delComment,
  addComment,
  updateComment,
  examineComment,
} from "@/api/model/comment";
import { listAllColumn } from "@/api/model/column";
export default {
  name: "Comment",
  dicts: ["source", "examine_status", "show_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        columnId: null,
        commentUserName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columnList: [],
    };
  },
  created() {
    this.getList();
    // this.getColumnList()
  },
  methods: {
    // getColumnList() {
    //   this.loading = true;
    //   listAllColumn({'status':1}).then(response => {
    //     this.columnList = response.data;
    //   });
    // },
    /** 查询评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then((response) => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        columnId: null,
        modelId: null,
        content: null,
        commentTime: null,
        commentUserId: null,
        commentUserName: null,
        source: null,
        examineUserId: null,
        examineStatus: null,
        examineTime: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDeleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getComment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateComment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该评论数据项？")
        .then(function () {
          return delComment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "model/comment/export",
        {
          ...this.queryParams,
        },
        `comment_${new Date().getTime()}.xlsx`
      );
    },
    reviewComment(row, index, action) {
      row.examineStatus = action ? "2" : "3";
      //审核评论
      examineComment(row).then(
        () => {
          this.getList();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          console.log(error);
        }
      );
    },
  },
};
</script>
